package com.it.yx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.yx.domain.News;
import com.it.yx.domain.Student;
import com.it.yx.domain.User;
import com.it.yx.mapper.NewsMapper;
import com.it.yx.mapper.UserMapper;
import com.it.yx.service.NewsService;
import com.it.yx.service.StudentService;
import com.it.yx.service.UserService;
import com.it.yx.tool.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/vue")
@RestController
public class AllController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StudentService studentService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private NewsMapper newsMapper;
  /**
   * 用户注册
   * @param user 实体类参数
   *             前后均可判断账号密码是否为空
   * @return 返回boolean 真 假
   */
  @PostMapping("enroll")
  public boolean userenroll(User user) {
        if (user.getUsername() != null||user.getPassword() != null){
            return userService.save(user);
        }else {
            return false;
        }
    }
  /**
   * 登录检验 判断炸账号密码师=是否与数据库相等
   * @param user
   *
   * @return
   */
  @PostMapping("logn")
  public boolean index(User user) {
      User one = userMapper.selectOne(new QueryWrapper<User>()
              .eq("username", user.getUsername())
              .eq("password", user.getPassword()));
      if (ObjectUtils.isEmpty(one)){
          return false;

      } else {
          return true;
       }
    }
  /**
   * 查询student 所有信息
   * @return */
  @GetMapping("liststudend")
  public R liststudend() {
//      List<Student>
      List<Student> list = studentService.list();
      return R.Yes("请求成功",list);
  }
  /**
   * 返回 news所有信息
   * @return */
  @GetMapping("listnews")
  public List<News> listnews() {
      return newsService.list();
  }

  /**
   * 返回新闻前三条数据
   * @return */
  @GetMapping("listthree")
  public List<News> listthree() {
      return newsMapper.selecTthree();
   }


}
